---------------------------------------
--          AmBContentPanel          --
---------------------------------------
-- Simple content panel. Intended to be the
-- base for all other AmB components.
local ContentPanel = {
   Init = function(self)
      self._PanelColor = Color(255, 255, 255, 0)
      self._Dock = false
		self._LastAnimate = CurTime()
   end,

   Paint = function(self)
      if self._PanelColor.a > 0 then
         SetSurfaceToColor(self._PanelColor)
         surface.DrawRect(0, 0, self:GetWide(), self:GetTall())
      end
      return false
   end,

   Think = function(self)
      --Docking
      if self._Dock then
         self:_Redock()
      end
      self:PanelThink()
		local lsa = self._LastAnimate
		local ct = CurTime()
		self:PanelAnimate(ct - lsa)
		self._LastAnimate = ct
   end,

   -- Use this function when thinking.
   PanelThink = function(self)
   end,

   -- Use this function for mouse events. Type
   -- is 1 for mousepressed, 2 for mousemoved and
   -- 3 for mousereleased. Code is standard mouse
   -- code. X and Y are self explainatory and if you
   -- think otherwise... you ARE an idiot.
   PanelOnMouse = function(self, Type, Code, X, Y)
      return false
   end,

   OnMousePressed = function(self, Code)
      self:_OnMouse(1, Code)
   end,

   OnCursorMoved = function(self, X, Y)
      self:_OnMouse(2, nil)
   end,

   OnMouseReleased = function(self, Code)
      self:_OnMouse(3, Code)
   end,

   --Sets if this panel should fit its parent
   SetDocking = function(self, Value)
      self._Dock = Value
      if self._Dock then
         self:_Redock()
      end
   end,

   --Tints the panel. This is usually a debug function
   SetPanelColor = function(self, Color)
      self._PanelColor = Color
   end,

   --Tries to resize a docked control by resizing parents. Returns
   --true if it was accepted.
   RequestResize = function(self, NewWidth, NewHeight)
      if self._Dock then
         local parent = self:GetParent()
         local f = parent.OnChildRequestResize
         if f != nil then
            local res = f(parent, NewWidth, NewHeight)
            if res then
               self:_Redock()
               return true
            else
               return false
            end
         end
      else
         self:SetSize(NewWidth, NewHeight)
         return true
      end
   end,

   --Response to a resize request. Returns true if the request
   --was accepted(and it should be).
   OnChildRequestResize = function(self, NewWidth, NewHeight)
      if not self._Dock then
         return false
      else
         if self:GetWide() != NewWidth and self:GetTall() != NewHeight then
            return self:RequestResize(NewWidth, NewHeight)
         else
            return true
         end
      end
   end,
	
	-- Called when the panel should be updated without user interaction.
	-- It is called with the amount of time in seconds that has passed
	-- since the last animate cycle.
	PanelAnimate = function(self, UpdateTime)
	end,

   _OnMouse = function(self, Type, Code)
      local f = self.PanelOnMouse
      if f != nil then
         local ret = f(self, Type, Code, self:CursorPos())
         if not ret then
            local parent = self:GetParent()
            if parent != nil then
               if parent._OnMouse != nil then
                  parent:_OnMouse(Type, Code, self:CursorPos())
               end
            end
         else
            -- To be more uniform... capture a mousepress if the event is handled
            if Type == 1 then
               self:MouseCapture(true)
            end
            if Type == 3 then
               self:MouseCapture(false)
            end
         end
      end
   end,

   _Redock = function(self)
      parent = self:GetParent()
      self:SetPos(0, 0)
      self:SetSize(parent:GetWide(), parent:GetTall())
   end
}
AmB.UI.ContentPanel = ContentPanel
vgui.Register("AmBContentPanel", ContentPanel, "Panel")